<!-- 桌台列表 -->
<template>
  <BasicTable @register="registerTable" :rowSelection="rowSelection">
    <template #tableTitle>
      <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleAddTable"> 添加桌台</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <template #overlay>
          <a-menu>
            <a-menu-item key="1" @click="batchHandleDelete">
              <Icon icon="ant-design:delete-outlined"></Icon>
              删除
            </a-menu-item>

            <a-menu-item key="1" @click="batchHandleDelete">
              <Icon icon="ant-design:download-outlined"></Icon>
              下载桌码
            </a-menu-item>
          </a-menu>
        </template>
        <a-button>批量操作
          <Icon icon="mdi:chevron-down"></Icon>
        </a-button>
      </a-dropdown>
    </template>
    <!--操作栏-->
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" />
    </template>
  </BasicTable>
  <!--抽屉-->
  <TableDrawer @register="registerDrawer" @success="handleSuccess" showFooter/>
</template>

<script lang="ts" name="shop-table" setup>
//ts语法
import {onMounted, ref, unref} from 'vue';
import {ActionItem, BasicTable, TableAction} from '/@/components/Table';
import TableDrawer from './TableDrawer.vue';
import {useListPage} from '/@/hooks/system/useListPage'
import {useDrawer} from '/@/components/Drawer';
import {useMessage} from '/@/hooks/web/useMessage';
import {columns, searchFormSchema} from './table.data';
import {batchDeleteTable, deleteTable, listTable} from './table.api';
import {listStore} from '../../storeList/store.api';
import {useUserStore} from '/@/store/modules/user';

const { createMessage, createConfirm } = useMessage();

//注册drawer
const [registerDrawer, { openDrawer }] = useDrawer();
const tenantId = ref(null)
// 列表页面公共参数、方法
const { tableContext } = useListPage({
  designScope: 'table-list',
  tableProps: {
    title: '桌台列表',
    api: listTable,
    bordered: false,
    immediate: false,
    columns: columns,
    pagination: false,
    useSearchForm: true,
    tableSetting: { fullScreen: true },
    formConfig: {
      schemas: searchFormSchema,
      showResetButton: false
    },
    actionColumn: {
      width: 120,
    },
    beforeFetch: (params) => {
      params.tenantId = unref(tenantId.value)
    },
  }
})

const [registerTable, { reload, getForm }, { rowSelection, selectedRowKeys }] = tableContext


onMounted(() => {
  const { updateSchema } = getForm()
  const userStore = useUserStore()
  if (!userStore.getSelectTenant.isMainTenant) {
    reload()
    return
  }
  updateSchema({
    field: 'tenantId',
    componentProps: ({ formModel, formActionType }) => {
      return {
        optionFilterProp: "label",
        labelField: 'name',
        valueField: 'tenantId',
        showSearch: true,
        api: listStore,
        onSelect: async (value: any, option: any) => {
          console.log("option", option);
          tenantId.value = value
          reload()
        },
        onOptionsChange: async (options) => {
          if (options.length > 0) {
            formModel.tenantId = options[0].value
            tenantId.value = options[0].value
            console.log('tenantId', tenantId.value);
            reload()
          }
        }
      }
    },
  })
})

/**
 * 添加
 */
function handleAddTable() {
  openDrawer(true, { tenantId: tenantId.value, isUpdate: false})
}


/**
 * 编辑事件
 */
async function handleEdit(record: Recordable) {
  openDrawer(true, { record, isUpdate: true, tenantId: tenantId.value });
}

/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteTable({ id: record.id }, reload);
}
/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  await batchDeleteTable({ ids: selectedRowKeys.value }, () => {
    selectedRowKeys.value = []
    reload()
  })
}
/**
 * 成功回调
 */
function handleSuccess({ values }) {
  reload()
  createMessage.success("编辑成功")
}

/**
 * 操作栏
 */
function getTableAction(record): ActionItem[] {
  return [
  {
      label: '桌码',
      onClick: handleEdit.bind(null, record)
    },
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record)
    },
    {
      label: '删除',
      popConfirm: {
        title: `是否确认删除 ${record.code}`,
        confirm: handleDelete.bind(null, record)
      },
    },
  ]
}

</script>

<style scoped></style>
